package com.rd.student.jike.class2.lihongming.week724_730.Day725;

/**
 * @Auther:Heinrich
 * @Date:2023/7/24-07-24-9:17
 * @Description:com.rd.student.jike.class2.lihongming.Day0724
 * @version:1.0
 */
public class Question6 {
    public static Integer Num = 1;
    public static int temp = 0;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                    synchronized (this) {
                        while (temp != 0) {
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        for (int i = 0; i < 5; i++) {
                            System.out.println(Thread.currentThread().getName() + " " + Num++);
                        }
                        temp = 1;
                        this.notifyAll();
                    }

            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {

                    synchronized (this) {
                        while (temp != 1) {
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        for (int i = 0; i < 5; i++) {
                            System.out.println(Thread.currentThread().getName() + " " + Num++);
                        }
                        temp = 2;
                        this.notifyAll();
                    }

            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                    synchronized (this) {
                        while (temp != 2) {
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        for (int i = 0; i < 5; i++) {
                            System.out.println(Thread.currentThread().getName() + " " + Num++);
                        }
                        temp = 0;
                        this.notifyAll();
                    }
                }
        }).start();

    }

}
class Print implements Runnable{
    public Integer Num = 1;
    public int temp = 0;

    @Override
    public void run() {
        synchronized (this) {
            if (temp != 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " " + Num++);
            }
            temp=1;
            this.notifyAll();
            if (temp != 1) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " " + Num++);
            }
            temp=2;
            this.notifyAll();
            if (temp != 2) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " " + Num++);
            }
            temp=0;
            this.notifyAll();
        }
    }
}